package com.luhaoran.hrm.controller;
import com.luhaoran.hrm.entity.User;
import com.luhaoran.hrm.service.IUserService;
import com.luhaoran.hrm.utils.ViewUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    IUserService userService;
    @Resource
    HttpServletRequest request;
    @Resource
    HttpServletResponse response;
    /**
     * 用户列表
     * @return
     * */
    @PostMapping
    public Object index(){
        List<User> users = userService.getList();
        return ViewUtil.json(response,200,"success",users);
    }
    /**
     *
     * 获取单个用户
     * @return
     */
    @GetMapping("get/{id}")
    public Object get(@PathVariable Integer id){
        log.info("接收到的id:"+id);
        User user = userService.getById(id);
        return ViewUtil.json(response,200,"success",user);
    }
    /**
     * 保存用户
     * @return
     * */
    @PostMapping("save")
    public Object save(@RequestBody User user){
        Integer userId = userService.saveUser(user);
        return ViewUtil.json(response,200,"success",user.getId());
    }
    /**
     *
     * 修改密码
     * @return
     * */
    @PostMapping("changePwd")
    public Object changePwd(@RequestBody String password){
        String id = request.getAttribute("id").toString();
        userService.changePwd(Integer.parseInt(id),password);
        return ViewUtil.json(response,200,"success");
    }

    /**
     * 移除用户
     * @param id
     * @return
     * */
    @GetMapping("remove/{id}")
    public Object remove(@PathVariable("id") Integer id){
        userService.removeUser(id);
        return ViewUtil.json(response,200,"success");
    }
}